import Vue from 'vue'
import store from '@/store/index'

import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

import {
    Component
} from 'vue-property-decorator';
// 注册路由钩子，避免ts无法触发
Component.registerHooks([
    'beforeRouteEnter',
    'beforeRouteLeave',
    'beforeRouteUpdate'
]);

import { linkVerify } from '@/utils/index'
// 全局注入方法
Vue.mixin({
    methods: {
        // 跳转到新页面
        go(name, params = {}, { isUp = 0, isBase = false, isClearDB = true } = {}) {
            const me = this;
            if (linkVerify(name)) {
                window.location.href = name;
            } else {
                if (isBase) {
                    // 记录当前页面路由名称，用于返回
                    params.baseUrl = me.$route.name;
                }
                // 默认标识当前不需要重载数据
                params.isUp = isUp;
                if (isClearDB) {
                    // 标识当前是否需要清空目标页面数据缓存
                    const id = params.id || '';
                    store.dispatch('coreDb/set', { path: `$page.${name}${id}`, value: null })
                }
                me.$router.push({
                    name,
                    params
                });
            }
        }
    }
})